/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao.locacao;

import modelo.persistencia.ControleAcesso;
import modelo.persistencia.PersistenciaLocacao;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import modelo.dominio.Locacao;
import modelo.dominio.Veiculo;
import modelo.persistencia.PersistenciaVeiculo;
import modelo.persistencia.PersistenciaLocacao;
/**
 *
 * @author gustavo
 */
public class InterfaceFinalizaLocacao extends javax.swing.JFrame{
	
	private final PersistenciaLocacao persistenciaLocacao = new PersistenciaLocacao();
	private final PersistenciaVeiculo persistenciaVeiculo = new PersistenciaVeiculo();
	
	public InterfaceFinalizaLocacao(){
		initComponents();
		
		this.setTitle("Finalizando Locação");
		
		jButton1.setVisible(true);
		jButton2.setVisible(true);
		jButton3.setVisible(true);
		jButton4.setVisible(false);
		jButton5.setVisible(false);
		jLabel1.setVisible(true);
		jLabel2.setVisible(false);
		jLabel3.setVisible(false);
		jLabel4.setVisible(false);
		jLabel5.setVisible(false);
		jLabel6.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEnabled(true);
		jTextField1.setText("");
		jTextField1.setVisible(true);
	     jTextField2.setVisible(false);
		jTextField3.setVisible(false);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Finalizando Locação", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu Light", 0, 24))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Ubuntu Light", 0, 18));
        jLabel1.setText("Placa : ");

        jButton3.setText("Ok");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Ubuntu Light", 0, 18));
        jLabel2.setText("Alterar data : ");

        jLabel3.setFont(new java.awt.Font("Ubuntu Light", 0, 18));
        jLabel3.setText("Km de Entrada : ");

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jButton5.setText("Confirmar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmarActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Ubuntu Light", 0, 18));
        jLabel4.setText("Para alterar a data de devolução preencha somente o campo de \"Data\" e confirme:");

        jLabel5.setFont(new java.awt.Font("Ubuntu Light", 0, 18));
        jLabel5.setText("Para finalizar a locação preencha o campo de \"Km de Entrada\" e confirme:");

        jLabel6.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(456, Short.MAX_VALUE)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(118, 118, 118)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(381, 381, 381))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 670, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 654, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(63, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(115, 115, 115)
                .addComponent(jLabel6)
                .addContainerGap(675, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(62, 62, 62)
                .addComponent(jLabel6)
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jButton1.setText("Logoff");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogoffActionPerformed(evt);
            }
        });

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 556, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 485, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(65, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
	
	
	private void jButtonConfirmarActionPerformed(java.awt.event.ActionEvent evt) {
		String placa = jTextField1.getText();
		String data = jTextField2.getText();
		String kmEntrada = jTextField3.getText();
		GregorianCalendar dataDev = new GregorianCalendar();
		
		if(placa.equals("") && kmEntrada.equals("")){
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Por favor, preencha pelo menos um campo!","Erro",JOptionPane.ERROR_MESSAGE);
		}
		if(!data.equals("") && kmEntrada.equals("")){
			jLabel6.setVisible(false);
			dataDev = persistenciaLocacao.converteData(data);
			Veiculo vec = new Veiculo(placa);
			Veiculo vec1 = (Veiculo) persistenciaVeiculo.consultar(vec);
			Locacao loc = new Locacao(vec1);
			Locacao loc1 = persistenciaLocacao.consultarLocacaoVeiculo(loc);
			loc1.setDataDevolucao(dataDev);
			loc1.setPrevisaoDias(persistenciaLocacao.calculaDiasDiferenca(loc1));
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Alteração concluída!","Parabéns!",JOptionPane.INFORMATION_MESSAGE);
		}
		if(data.equals("") && !kmEntrada.equals("")){
			long quilometragem = Long.parseLong(kmEntrada);
			Veiculo vec = new Veiculo(placa);
			Veiculo vec1 = (Veiculo) persistenciaVeiculo.consultar(vec);
			Locacao loc = new Locacao(vec1);
			Locacao loc1 = persistenciaLocacao.consultarLocacaoVeiculo(loc);
			loc1.setQuilometragemEntrada(quilometragem);
			loc1.setFinalizada(true);
			vec1.setDisponivel(true);
			persistenciaVeiculo.atualizar(vec1);
			double total = persistenciaLocacao.calcularValorLocacoes();
			loc1.setValorLocacao(total);
			persistenciaLocacao.atualizar(loc1);
			jLabel6.setText("Valor da locação : " + total);
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Locação finalizada!","Parabéns!",JOptionPane.INFORMATION_MESSAGE);
		}
	}

	private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {
		jButton1.setVisible(true);
		jButton2.setVisible(true);
		jButton3.setVisible(true);
		jButton4.setVisible(false);
		jButton5.setVisible(false);
		jLabel1.setVisible(true);
		jLabel2.setVisible(false);
		jLabel3.setVisible(false);
		jLabel4.setVisible(false);
		jLabel5.setVisible(false);
		jLabel6.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEnabled(true);
		jTextField1.setText("");
		jTextField1.setVisible(true);
		jTextField2.setText("");
	     jTextField2.setVisible(false);
		jTextField3.setText("");
		jTextField3.setVisible(false);
	}

	private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {
		String placa = jTextField1.getText();
		
		if(!placa.equals("")){
			Veiculo vec = new Veiculo(placa);
			Veiculo vec1 = (Veiculo) persistenciaVeiculo.consultar(vec);
			if(vec1 != null){
				Locacao loc = new Locacao(vec1);
				Locacao loc1 = persistenciaLocacao.consultarLocacaoVeiculo(loc);
				if(loc1 != null){
					if(loc1.isFinalizada() != true){
						jButton1.setVisible(true);
						jButton2.setVisible(true);
						jButton3.setVisible(true);
						jButton4.setVisible(true);
						jButton5.setVisible(true);
						jLabel1.setVisible(true);
						jLabel2.setVisible(true);
						jLabel3.setVisible(true);
						jLabel4.setVisible(true);
						jLabel5.setVisible(true);
						jLabel6.setVisible(false);
						jPanel1.setVisible(true);
						jTextField1.setEnabled(false);
						jTextField1.setVisible(true);
						jTextField2.setVisible(true);
						jTextField3.setVisible(true);
					}else{
						JOptionPane.showMessageDialog(null,"<html> <font color='black'>Locação já esta finalizada!","Parabéns!",JOptionPane.INFORMATION_MESSAGE);
						jButton1.setVisible(true);
						jButton2.setVisible(true);
						jButton3.setVisible(true);
						jButton4.setVisible(false);
						jButton5.setVisible(false);
						jLabel1.setVisible(true);
						jLabel2.setVisible(false);
						jLabel3.setVisible(false);
						jLabel4.setVisible(false);
						jLabel5.setVisible(false);
						jLabel6.setVisible(false);
						jPanel1.setVisible(true);
						jTextField1.setEnabled(true);
						jTextField1.setText("");
						jTextField1.setVisible(true);
						jTextField2.setVisible(false);
						jTextField3.setVisible(false);
					}
				}else{
					JOptionPane.showMessageDialog(null,"<html> <font color='black'>Não existe Locação com esse Veículo!","Erro",JOptionPane.ERROR_MESSAGE);
					jTextField1.setText("");
				}
			}else{
				JOptionPane.showMessageDialog(null,"<html> <font color='black'>Não existe Veículo com essa Placa!","Erro",JOptionPane.ERROR_MESSAGE);
				jTextField1.setText("");
			}
		}else{
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Digita uma Placa por favor!","Erro",JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jButtonLogoffActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
		ControleAcesso.logoff();
	}

	private void jButtonVoltarActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
		ControleAcesso.voltarMenuPrincipal();
	}

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private int evento;
}
